/*
 * Cherry programming language
 * Copyright (C) 2013 Christoph Mueller
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "cherry/runtime.h"

#include <gc.h>

// ----------------------------------------------------------------------------
// cherry object constructors
// ----------------------------------------------------------------------------

struct org_cherry_value* 	
__org_cherry_make_fixnum(cy_fixnum_t value)
{
	struct org_cherry_value* cy_value = GC_MALLOC(sizeof(struct org_cherry_value));
	cy_value->meta.type = CY_FIXNUM;
	cy_value->fixnum_value = value;
	return cy_value;
}


struct org_cherry_value* 	
__org_cherry_make_float(cy_float_t value)
{
	struct org_cherry_value* cy_value = GC_MALLOC(sizeof(struct org_cherry_value));
	cy_value->meta.type = CY_FLOAT;
	cy_value->float_value = value;
	return cy_value;
}


struct org_cherry_value* 	
__org_cherry_make_string(cy_byte_t* value)
{
	struct org_cherry_value* cy_value = GC_MALLOC(sizeof(struct org_cherry_value));
	cy_value->meta.type = CY_STRING;
	cy_value->string_value = value;
	return cy_value;
}


struct org_cherry_value* 	
__org_cherry_make_char(cy_unicode_t value)
{
	struct org_cherry_value* cy_value = GC_MALLOC(sizeof(struct org_cherry_value));
	cy_value->meta.type = CY_CHAR;
	cy_value->char_value = value;
	return cy_value;
}

// ----------------------------------------------------------------------------
// cherry list operations
// ----------------------------------------------------------------------------

struct org_cherry_pair*     
__org_cherry_list_cons(struct org_cherry_value* head, struct org_cherry_value* tail)
{
	struct org_cherry_pair* pair = GC_MALLOC(sizeof(struct org_cherry_pair));
	pair->meta.type = CY_PAIR;
	pair->head = head;
	pair->tail = tail;
	return pair;
}

struct org_cherry_value*    
__org_cherry_list_head(struct org_cherry_pair* pair)
{
	return pair->head;
}

struct org_cherry_value*    
__org_cherry_list_tail(struct org_cherry_pair* pair)
{
	return pair->tail;
}

struct org_cherry_value*    
__org_cherry_list_length(struct org_cherry_pair* pair)
{
	cy_fixnum_t length = 0;

	while(pair->meta.type == CY_PAIR) {
		length++;
		pair = (struct org_cherry_pair*) pair->tail;
	}

	return __org_cherry_make_fixnum(length);
}

struct org_cherry_value*
__org_cherry_primitive_add(struct org_cherry_pair* pair)
{
	cy_fixnum_t result = 0;

	while(pair->meta.type == CY_PAIR) {
		result += pair->head->fixnum_value;
		pair = (struct org_cherry_pair*) pair->tail;
	}

	return __org_cherry_make_fixnum(result);
}


